﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using MSAP.Framework.Interfaces;
using System.ComponentModel;

namespace MSAP.Framework.Controls
{
    /// <summary>
    /// Interaction logic for ActionsControl.xaml
    /// </summary>
    public partial class ActionsControl : UserControl
    {
        public ActionsControl()
        {
            InitializeComponent();
            
        }

        /// <summary>
        /// On initilise uniquement lors du premier appel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (m_FirstLoad == true)
            {
                m_FirstLoad = false;
                InitUserControl();
            }
        }

        /// <summary>
        /// Initialise le contenu du control
        /// </summary>
        private void InitUserControl()
        {
            if (ItemsSource == null) return;

            List<string> lstGroupID = null;
            if (string.IsNullOrEmpty(GroupsID) == false)
            {
                string[] tabGroupID = GroupsID.Split(new char[] { ',' });
                lstGroupID = new List<string>(tabGroupID);
            }

            int i = 0;
            foreach (IServiceAction action in ItemsSource)
            {
                if (action.CheckAccessForRole(action.Shell.UserRole) == false)
                    continue; // Le role de l'utilisateur interdit l'action

                //Si la liste est null alors on prend tous les groupID en compte.
                if (lstGroupID == null || (lstGroupID != null && lstGroupID.Contains(action.GroupID) == true))
                {
                    Button button = new Button();
                    button.Name = action.ID;
                    button.Content = action.Title;
                    button.Command = action.Command;
                    button.ToolTip = action.Comments;
                    button.Margin = new Thickness(2.0);

                    if (action.GroupID.Contains("NAVIGATE"))
                    {
                        button.OverridesDefaultStyle = true;
                        button.MinWidth = 10.0;
                    }

                    if (CollapseIfNotEnabled == true)
                    {
                        Binding bind = new Binding("IsEnabled");
                        bind.Source = button;
                        bind.Converter = new VisibilityConverter();
                        DependencyProperty visibilityProp = Button.VisibilityProperty;
                        BindingOperations.SetBinding(button, visibilityProp, bind);
                    }

                    if (action.Visible == false)
                        button.Visibility = Visibility.Collapsed;

                    button.Click += new RoutedEventHandler(button_Click);
                    Panel.Children.Add(button);
                    i++;
                }
            }
        }

        #region Liste des DependencyProperty
        
        public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable<IServiceAction>), typeof(ActionsControl));

        public static readonly RoutedEvent CommandExecutedEvent = EventManager.RegisterRoutedEvent(
            "CommandExecuted", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(ActionsControl));

        #endregion

        #region Properties

        /// <summary>
        /// Liste des groupID dont l'ActionControl doit afficher les boutons. Pour les prendre tous ne pas mettre cette property
        /// La liste doit être de la forme GroupID1,GroupID2,etc
        /// </summary>
        public string GroupsID
        {
            get;
            set;
        }

        /// <summary>
        /// Liste des groupID dont l'ActionControl doit afficher les boutons. Pour les prendre tous ne pas mettre cette property
        /// La liste doit être de la forme GroupID1,GroupID2,etc
        /// </summary>
        public bool CollapseIfNotEnabled
        {
            get;
            set;
        }

        /// <summary>
        /// Objet de type IEnumerable<IServiceAction> qui contient la liste des actions.
        /// </summary>
        public IEnumerable<IServiceAction> ItemsSource
        {
            get { return (IEnumerable<IServiceAction>)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        #endregion

        #region private fields
        
        private bool m_FirstLoad = true;
        
        #endregion

        public event RoutedEventHandler CommandExecuted
        {
            add { AddHandler(CommandExecutedEvent, value); }
            remove { RemoveHandler(CommandExecutedEvent, value); }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(CommandExecutedEvent, sender));
        }
    }
}
